package org.fh.controller.fhoa;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.act.AcStartController;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.ApplyforloanService;
import org.fh.util.Const;
import org.fh.util.DateUtil;
import org.fh.util.FileUpload;
import org.fh.util.Jurisdiction;
import org.fh.util.ObjectExcelView;
import org.fh.util.PathUtil;
import org.fh.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;



@Controller
@RequestMapping("/applyforloan")
public class ApplyforloanController extends AcStartController{

	@Autowired
	private ApplyforloanService applyforloanService;

	@Autowired
	private HttpServletRequest request;

	/**保存
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	@RequiresPermissions("applyforloan:add")
	@ResponseBody
	public Object add(
			@RequestParam(value="FIMGA",required=false) MultipartFile filea,
			@RequestParam(value="BEIGINTIME",required=false) String BEIGINTIME,
			@RequestParam(value="ENDTIME",required=false) String ENDTIME,
			@RequestParam(value="LOANTITLE",required=false) String LOANTITLE,
			@RequestParam(value="LOANDESC",required=false) String LOANDESC,
			@RequestParam(value="AMOUNT",required=false) String AMOUNT
	) throws Exception{

		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		String  ffile = DateUtil.getDays(), fileNamea = "";
		String filePath =PathUtil.getHttpUrlpathFile()+ Const.FILEPATHIMG + ffile;	 //文件上传路径
		String readFilePath=PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;

		if (null != filea && !filea.isEmpty()) {
			fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());				//执行上传
			pd.put("PHOTOADDA", readFilePath+ "/" + fileNamea);
		}


		//写入表
		pd.put("BEIGINTIME", BEIGINTIME);
		pd.put("ENDTIME", ENDTIME);
		pd.put("LOANTITLE", LOANTITLE);
		pd.put("LOANDESC", LOANDESC);
		pd.put("AMOUNT", AMOUNT);
		pd.put("CTIME", DateUtil.date2Str(new Date()));	//创建时间
		pd.put("USERNAME", Jurisdiction.getUsername());	//
		pd.put("LOAN_ID", this.get32UUID());					//主键
		pd.put("UNAME", Jurisdiction.getName());
		pd.put("USERNAME", Jurisdiction.getUsername());

		pd.put("DEPARTMENT_ID", "");
//			pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());


		applyforloanService.save(pd);

		try {
			/** 工作流的操作 **/
			Map<String,Object> mapa = new LinkedHashMap<String, Object>();
			mapa.put("申请人员", Jurisdiction.getName());			//当前用户的姓名
			mapa.put("开始时间", BEIGINTIME);
			mapa.put("结束时间", ENDTIME);
			mapa.put("申请标题", LOANTITLE);
			mapa.put("申请内容", LOANDESC);
			mapa.put("申请金额", AMOUNT);
			String USERNAME = Jurisdiction.getUsername();
			mapa.put("USERNAME", USERNAME);						//指派代理人为当前用户
			startProcessInstanceByKeyHasVariables("KEY_leavejk",mapa,USERNAME);//启动流程实例(请假单流程)通过KEY
			//记录存入数据库
			map.put("ASSIGNEE_",Jurisdiction.getUsername());				//用于给待办人发送新任务消息
		} catch (Exception e) {
			errInfo = "errer";
		}

		map.put("result", errInfo);				//返回结果
		return map;
	}


	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@RequiresPermissions("applyforloan:del")
	@ResponseBody
	public Object delete() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		applyforloanService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}


	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	@RequiresPermissions("applyforloan:edit")
	@ResponseBody
	public Object edit(
			@RequestParam(value="PHOTOADDA",required=false) MultipartFile filea,
			@RequestParam(value="BEIGINTIME",required=false) String BEIGINTIME,
			@RequestParam(value="ENDTIME",required=false) String ENDTIME,
			@RequestParam(value="LOANTITLE",required=false) String LOANTITLE,
			@RequestParam(value="LOANDESC",required=false) String LOANDESC,
			@RequestParam(value="AMOUNT",required=false) String AMOUNT,
			@RequestParam(value="LOAN_ID",required=false) String LOAN_ID
	) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		String  ffile = DateUtil.getDays(), fileNamea = "";
		String filePath =PathUtil.getHttpUrlpathFile()+ Const.FILEPATHIMG + ffile;	 //文件上传路径
		String readFilePath=PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;



		if (null != filea && !filea.isEmpty()) {
			fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());				//执行上传
			pd.put("PHOTOADDA", readFilePath+ "/" + fileNamea);
		}



		pd.put("BEIGINTIME", BEIGINTIME);
		pd.put("ENDTIME", ENDTIME);
		pd.put("LOANTITLE", LOANTITLE);
		pd.put("LOANDESC", LOANDESC);
		pd.put("AMOUNT", AMOUNT);
		pd.put("CTIME", DateUtil.date2Str(new Date()));	//创建时间
		pd.put("USERNAME", Jurisdiction.getUsername());	//
		pd.put("LOAN_ID", LOAN_ID);					//主键
//		pd.put("UNAME", Jurisdiction.getName());
//		pd.put("USERNAME", Jurisdiction.getUsername());
//		pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		applyforloanService.edit(pd);

		map.put("result", errInfo);				//返回结果
		return map;
	}
	/**去修改页面
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	@ResponseBody
	public Object goEdit()throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = applyforloanService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@RequiresPermissions("applyforloan:list")
	@ResponseBody
	public Object list(Page page) throws Exception{
//		Map<String,Object> map = new HashMap<String,Object>();
//		String errInfo = "success";
//		PageData pd = new PageData();
//		pd = this.getPageData();
//
//		String USERNAME = Jurisdiction.getUsername();					//用户名
//		page.setPd(pd);
//		List<PageData>	varList = applyforloanService.list(page);
//		map.put("varList", varList);
//		map.put("page", page);
//		map.put("USERNAME", USERNAME);
//		map.put("result", errInfo);
//		return map;
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String isMy = pd.getString("isMy");
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		String lastStart = pd.getString("lastStart");					//开始时间
		String lastEnd = pd.getString("lastEnd");						//结束时间
		if(Tools.notEmpty(lastStart))pd.put("lastStart", lastStart+" 00:00:00");
		if(Tools.notEmpty(lastEnd))pd.put("lastEnd", lastEnd+" 00:00:00");
		String USERNAME = Jurisdiction.getUsername();					//用户名
		String item = Jurisdiction.getDEPARTMENT_IDS();
		if("false".equals(isMy)){
			if("".equals(item) || "无权".equals(item)){
				pd.put("item","");											//根据部门ID过滤
				if(!"admin".equals(USERNAME))pd.put("USERNAME", USERNAME);	//非admin用户时
			}else{
				pd.put("ORUSERNAME", USERNAME);
				pd.put("item", item.replaceFirst("\\(", "\\('"+Jurisdiction.getDEPARTMENT_ID()+"',"));
			}
		}else {
			pd.put("USERNAME", USERNAME);
		}
		page.setPd(pd);
//		System.out.println("username===="+USERNAME);
//		System.out.println("isMy---"+isMy);
		List<PageData>	varList = applyforloanService.list(page);			//列出Worklog列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("USERNAME", USERNAME);
		map.put("result", errInfo);
		return map;
	}

	/**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/excel")
	@RequiresPermissions("toExcel")
	public ModelAndView exportExcel() throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		Map<String,Object> dataMap = new HashMap<String,Object>();
		List<String> titles = new ArrayList<String>();
		titles.add("借款开始时间");	//1
		titles.add("借款结束时间");	//2
		titles.add("借款标题");	//3
		titles.add("借款说明");	//5
		titles.add("借款金额");	//6
		titles.add("上传照片地址一");	//7
		titles.add("姓名");	//12
		titles.add("创建时间");	//13
		dataMap.put("titles", titles);
		List<PageData> varOList = applyforloanService.listAll(pd);
		List<PageData> varList = new ArrayList<PageData>();
		for(int i=0;i<varOList.size();i++){
			PageData vpd = new PageData();
			vpd.put("var1", varOList.get(i).getString("BEIGINTIME"));	    //1
			vpd.put("var2", varOList.get(i).getString("ENDTIME"));	    //2
			vpd.put("var3", varOList.get(i).getString("LOANTITLE"));	    //2
			vpd.put("var4", varOList.get(i).getString("LOANDESC"));	    //2
			vpd.put("var5", varOList.get(i).getString("AMOUNT"));	    //2
			vpd.put("var6", varOList.get(i).getString("PHOTOADDA"));	    //2
			vpd.put("var7", varOList.get(i).getString("USERNAME"));	    //3
			vpd.put("var8", varOList.get(i).getString("CTIME"));	    //4
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}
}
